import matplotlib.pyplot as plt
import numpy as np
# path = file_path()
# path1 = file_path() +'result_data.log'
# f = open(path1,'w')
def miss_count(num_job,mid_result,edf_result,ins_result,srpt_result,feedback_result,thresh_result,opt_feedback_result,rm_result,srpt_edf_result,srpt_all_result,fifo_result,fifo_edf_result,file_name):
    path1 = file_name +'result_data.log'
    f = open(path1,'a')
    ave_mid_count =  ave_count(mid_result,num_job)
    ave_edf_count =  ave_count(edf_result,num_job)
    ave_ins_count =  ave_count(ins_result,num_job)
    ave_srpt_count = ave_count(srpt_result,num_job)
    ave_feed_count = ave_count(feedback_result,num_job)
    ave_thresh_count = ave_count(thresh_result,num_job)
    ave_optfeed_count = ave_count(opt_feedback_result,num_job)
    ave_rm_count = ave_count(rm_result,num_job)
    ave_srptedf_count = ave_count(srpt_edf_result,num_job)
    ave_srptall_count = ave_count(srpt_all_result,num_job)
    ave_fifo_count = ave_count(fifo_result,num_job)
    ave_fifoedf_count = ave_count(fifo_edf_result,num_job)
    x_data = ['MID','EDF','INS','SRPT','FB','THR','OPTFB','RM','SEDF','SALL','FIFO','FEDF']
    print("algorithm:",x_data,file=f)
    ind = np.arange(len(x_data))
    y_data1 = [ave_mid_count,ave_edf_count,ave_ins_count,ave_srpt_count,ave_feed_count,ave_thresh_count,ave_optfeed_count,ave_rm_count,ave_srptedf_count,ave_srptall_count,ave_fifo_count,ave_fifoedf_count]
    print("average miss ddl:",y_data1,file=f)
    y_data2 = [worst_miss(mid_result,num_job),worst_miss(edf_result,num_job),worst_miss(ins_result,num_job),worst_miss(srpt_result,num_job),worst_miss(feedback_result,num_job),worst_miss(thresh_result,num_job),worst_miss(opt_feedback_result,num_job),worst_miss(rm_result,num_job),worst_miss(srpt_edf_result,num_job),worst_miss(srpt_all_result,num_job),worst_miss(fifo_result,num_job),worst_miss(fifo_edf_result,num_job)]
    print("worst miss ddl:",y_data2,file=f)
    w = 0.2
    ax = plt.subplot(111)
    a = ax.bar(ind,y_data1,width=0.2,align='center')
    b = ax.bar(ind+w,y_data2,width=0.2,align='center')
    ax.set_xticks(ind+w)
    ax.set_xticklabels( ('MID','EDF','INS','SRPT','FB','THR','OPTFB','RM','SEDF','SALL','FIFO','FEDF') )
    plt.legend((a,b),('ave miss','worst miss'))
    plt.autoscale(tight=True)
    plt.title('The average/worst number of jobs that missed their deadlines')
    plt.savefig(file_name+'miss_count.png')
    # plt.show()
    plt.close()
    

def ave_count(result,num_job):
    count = 0
    for i in range(len(result)):
        count += num_job[i] - result[i]
    avecount = count/len(result)
    return avecount

def ave_cr(result,num_job):
    cr = 0
    for i in range(len(result)):
        cr += result[i]/num_job[i]
    avecr = cr/len(result)
    return avecr

def worst_miss(result,num_job):
    w_miss = 0
    for i in range(len(result)):
        if num_job[i]-result[i] >= w_miss:
            w_miss = num_job[i]-result[i]
    return w_miss

def cr(num_job,mid_result,edf_result,ins_result,srpt_result,feedback_result,thresh_result,opt_feedback_result,rm_result,srpt_edf_result,srpt_all_result,fifo_result,fifo_edf_result,file_name):
    path1 = file_name +'result_data.log'
    f = open(path1,'a')
    ave_mid_cr = ave_cr(mid_result,num_job)
    ave_edf_cr = ave_cr(edf_result,num_job)
    ave_ins_cr = ave_cr(ins_result,num_job)
    ave_srpt_cr = ave_cr(srpt_result,num_job)
    ave_feed_cr = ave_cr(feedback_result,num_job)
    ave_thresh_cr = ave_cr(thresh_result,num_job)
    ave_optfeed_cr = ave_cr(opt_feedback_result,num_job)
    ave_rm_cr = ave_cr(rm_result,num_job)
    ave_srptedf_cr = ave_cr(srpt_edf_result,num_job)
    ave_srptall_cr = ave_cr(srpt_all_result,num_job)
    ave_fifo_cr = ave_cr(fifo_result,num_job)
    ave_fifo_edf_cr = ave_cr(fifo_edf_result,num_job)
    x_data = ['MID','EDF','INS','SRPT','FB','THR','OPTFB','RM','SEDF','SALL','FIFO','FEDF']
    y_data = [ave_mid_cr,ave_edf_cr,ave_ins_cr,ave_srpt_cr,ave_feed_cr,ave_thresh_cr,ave_optfeed_cr,ave_rm_cr,ave_srptedf_cr,ave_srptall_cr,ave_fifo_cr,ave_fifo_edf_cr]
    plt.bar(x_data,y_data)
    print("average cr:",y_data,file=f)
    plt.title('The average completion rate')
    plt.savefig(file_name+'ave_cr.png')
    # plt.show()
    plt.close()


def AveUS(result):
    us = 0
    for i in range(len(result)):
        us += result[i]
    ave_us = us/len(result)
    return ave_us

def WorstUS(result):
    w_us = 1
    for i in range(len(result)):
        if result[i] <= w_us:
            w_us = result[i]
    return w_us

def AveCompRatio(result):
    comp_ratio = 0
    for i in range(len(result)):
        comp_ratio += result[i]
    ave_comp_ratio = comp_ratio/len(result)
    return ave_comp_ratio

def WorstCompRatio(result):
    w_CompRatio = 1
    for i in range(len(result)):
        if result[i] <= w_CompRatio:
            w_CompRatio = result[i]
    return w_CompRatio

def AveACT(result):
    ACT = 0
    for i in range(len(result)):
        ACT += result[i]
    ave_ACT = ACT/len(result)
    return ave_ACT

def WorstACT(result):
    w_ACT = 0
    for i in range(len(result)):
        if result[i] >= w_ACT:
            w_ACT = result[i]
    return w_ACT

def ave_us(US_mid,US_edf,US_ins,US_srpt,US_srpt_all,US_srpt_edf,US_feedback,US_thresh,US_opt_feedback,US_rm,US_fifo,US_fifo_edf,file_name):
    path1 = file_name +'result_data.log'
    f = open(path1,'a')
    ave_us_mid = AveUS(US_mid)
    ave_us_edf = AveUS(US_edf)
    ave_us_ins = AveUS(US_ins)
    ave_us_srpt = AveUS(US_srpt)
    ave_us_srpt_all = AveUS(US_srpt_all)
    ave_us_srpt_edf = AveUS(US_srpt_edf)
    ave_us_feedback = AveUS(US_feedback)
    ave_us_thresh = AveUS(US_thresh)
    ave_us_opt_feedback = AveUS(US_opt_feedback)
    ave_us_rm = AveUS(US_rm)
    ave_us_fifo = AveUS(US_fifo)
    ave_us_fedf = AveUS(US_fifo_edf)
    x_data = ['MID','EDF','INS','SRPT','FB','THR','OPTFB','RM','SEDF','SALL','FIFO','FEDF']
    y_data = [ave_us_mid,ave_us_edf,ave_us_ins,ave_us_srpt,ave_us_feedback,ave_us_thresh,ave_us_opt_feedback,ave_us_rm,ave_us_srpt_edf,ave_us_srpt_all,ave_us_fifo,ave_us_fedf]
    print("average us:",y_data,file=f)

def worst_us(US_mid,US_edf,US_ins,US_srpt,US_srpt_all,US_srpt_edf,US_feedback,US_thresh,US_opt_feedback,US_rm,US_fifo,US_fifo_edf,file_name):
    path1 = file_name +'result_data.log'
    f = open(path1,'a')
    worst_us_mid = WorstUS(US_mid)
    worst_us_edf = WorstUS(US_edf)
    worst_us_ins = WorstUS(US_ins)
    worst_us_srpt = WorstUS(US_srpt)
    worst_us_srpt_all = WorstUS(US_srpt_all)
    worst_us_srpt_edf = WorstUS(US_srpt_edf)
    worst_us_feedback = WorstUS(US_feedback)
    worst_us_thresh = WorstUS(US_thresh)
    worst_us_opt_feedback = WorstUS(US_opt_feedback)
    worst_us_rm = WorstUS(US_rm)
    worst_us_fifo = WorstUS(US_fifo)
    worst_us_fedf = WorstUS(US_fifo_edf)
    x_data = ['MID','EDF','INS','SRPT','FB','THR','OPTFB','RM','SEDF','SALL','FIFO','FEDF']
    y_data = [worst_us_mid,worst_us_edf,worst_us_ins,worst_us_srpt,worst_us_feedback,worst_us_thresh,worst_us_opt_feedback,worst_us_rm,worst_us_srpt_edf,worst_us_srpt_all,worst_us_fifo,worst_us_fedf]
    print("worst us:",y_data,file=f)

def ave_comp_ratio(CompRatio_mid,CompRatio_edf,CompRatio_ins,CompRatio_srpt,CompRatio_srpt_all,CompRatio_srpt_edf,CompRatio_feedback,CompRatio_thresh,CompRatio_opt_feedback,CompRatio_rm,CompRatio_fifo,CompRatio_fedf,file_name):
    path1 = file_name +'result_data.log'
    f = open(path1,'a')
    ave_compratio_mid = AveCompRatio(CompRatio_mid)
    ave_compratio_edf = AveCompRatio(CompRatio_edf)
    ave_compratio_ins = AveCompRatio(CompRatio_ins)
    ave_compratio_srpt = AveCompRatio(CompRatio_srpt)
    ave_compratio_srpt_all = AveCompRatio(CompRatio_srpt_all)
    ave_compratio_srpt_edf = AveCompRatio(CompRatio_srpt_edf)
    ave_compratio_feedback = AveCompRatio(CompRatio_feedback)
    ave_compratio_thresh = AveCompRatio(CompRatio_thresh)
    ave_compratio_opt_feedback = AveCompRatio(CompRatio_opt_feedback)
    ave_compratio_rm = AveCompRatio(CompRatio_rm)
    ave_compratio_fifo = AveCompRatio(CompRatio_fifo)
    ave_compratio_fedf = AveCompRatio(CompRatio_fedf)
    x_data = ['MID','EDF','INS','SRPT','FB','THR','OPTFB','RM','SEDF','SALL','FIFO','FEDF']
    y_data = [ave_compratio_mid,ave_compratio_edf,ave_compratio_ins,ave_compratio_srpt,ave_compratio_feedback,ave_compratio_thresh,ave_compratio_opt_feedback,ave_compratio_rm,ave_compratio_srpt_edf,ave_compratio_srpt_all,ave_compratio_fifo,ave_compratio_fedf]
    print("average compratio:",y_data,file=f)

def worst_comp_ratio(CompRatio_mid,CompRatio_edf,CompRatio_ins,CompRatio_srpt,CompRatio_srpt_all,CompRatio_srpt_edf,CompRatio_feedback,CompRatio_thresh,CompRatio_opt_feedback,CompRatio_rm,CompRatio_fifo,CompRatio_fedf,file_name):
    path1 = file_name +'result_data.log'
    f = open(path1,'a')
    worst_compratio_mid = WorstCompRatio(CompRatio_mid)
    worst_compratio_edf = WorstCompRatio(CompRatio_edf)
    worst_compratio_ins = WorstCompRatio(CompRatio_ins)
    worst_compratio_srpt = WorstCompRatio(CompRatio_srpt)
    worst_compratio_srpt_all = WorstCompRatio(CompRatio_srpt_all)
    worst_compratio_srpt_edf = WorstCompRatio(CompRatio_srpt_edf)
    worst_compratio_feedback = WorstCompRatio(CompRatio_feedback)
    worst_compratio_thresh = WorstCompRatio(CompRatio_thresh)
    worst_compratio_opt_feedback = WorstCompRatio(CompRatio_opt_feedback)
    worst_compratio_rm = WorstCompRatio(CompRatio_rm)
    worst_compratio_fifo = WorstCompRatio(CompRatio_fifo)
    worst_compratio_fedf = WorstCompRatio(CompRatio_fedf)
    x_data = ['MID','EDF','INS','SRPT','FB','THR','OPTFB','RM','SEDF','SALL','FIFO','FEDF']
    y_data = [worst_compratio_mid,worst_compratio_edf,worst_compratio_ins,worst_compratio_srpt,worst_compratio_feedback,worst_compratio_thresh,worst_compratio_opt_feedback,worst_compratio_rm,worst_compratio_srpt_edf,worst_compratio_srpt_all,worst_compratio_fifo,worst_compratio_fedf]
    print("worst compratio:",y_data,file=f)

def ave_ACT(ACT_mid,ACT_edf,ACT_ins,ACT_srpt,ACT_srpt_all,ACT_srpt_edf,ACT_feedback,ACT_thresh,ACT_opt_feedback,ACT_rm,ACT_fifo,ACT_fifo_edf,file_name):
    path1 = file_name +'result_data.log'
    f = open(path1,'a')
    ave_ACT_mid = AveACT(ACT_mid)
    ave_ACT_edf = AveACT(ACT_edf)
    ave_ACT_ins = AveACT(ACT_ins)
    ave_ACT_srpt = AveACT(ACT_srpt)
    ave_ACT_srpt_all = AveACT(ACT_srpt_all)
    ave_ACT_srpt_edf = AveACT(ACT_srpt_edf)
    ave_ACT_feedback = AveACT(ACT_feedback)
    ave_ACT_thresh = AveACT(ACT_thresh)
    ave_ACT_opt_feedback = AveACT(ACT_opt_feedback)
    ave_ACT_rm = AveACT(ACT_rm)
    ave_ACT_fifo = AveACT(ACT_fifo)
    ave_ACT_fedf = AveACT(ACT_fifo_edf)
    x_data = ['MID','EDF','INS','SRPT','FB','THR','OPTFB','RM','SEDF','SALL','FIFO','FEDF']
    y_data = [ave_ACT_mid,ave_ACT_edf,ave_ACT_ins,ave_ACT_srpt,ave_ACT_feedback,ave_ACT_thresh,ave_ACT_opt_feedback,ave_ACT_rm,ave_ACT_srpt_edf,ave_ACT_srpt_all,ave_ACT_fifo,ave_ACT_fedf]
    print("average ACT:",y_data,file=f)

def worst_ACT(ACT_mid,ACT_edf,ACT_ins,ACT_srpt,ACT_srpt_all,ACT_srpt_edf,ACT_feedback,ACT_thresh,ACT_opt_feedback,ACT_rm,ACT_fifo,ACT_fifo_edf,file_name):
    path1 = file_name +'result_data.log'
    f = open(path1,'a')
    worst_ACT_mid = WorstACT(ACT_mid)
    worst_ACT_edf = WorstACT(ACT_edf)
    worst_ACT_ins = WorstACT(ACT_ins)
    worst_ACT_srpt = WorstACT(ACT_srpt)
    worst_ACT_srpt_all = WorstACT(ACT_srpt_all)
    worst_ACT_srpt_edf = WorstACT(ACT_srpt_edf)
    worst_ACT_feedback = WorstACT(ACT_feedback)
    worst_ACT_thresh = WorstACT(ACT_thresh)
    worst_ACT_opt_feedback = WorstACT(ACT_opt_feedback)
    worst_ACT_rm = WorstACT(ACT_rm)
    worst_ACT_fifo = WorstACT(ACT_fifo)
    worst_ACT_fedf = WorstACT(ACT_fifo_edf)
    x_data = ['MID','EDF','INS','SRPT','FB','THR','OPTFB','RM','SEDF','SALL','FIFO','FEDF']
    y_data = [worst_ACT_mid,worst_ACT_edf,worst_ACT_ins,worst_ACT_srpt,worst_ACT_feedback,worst_ACT_thresh,worst_ACT_opt_feedback,worst_ACT_rm,worst_ACT_srpt_edf,worst_ACT_srpt_all,worst_ACT_fifo,worst_ACT_fedf]
    print("worst ACT:",y_data,file=f)